package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.Result;
import com.itheima.reggie.dto.OrdersDto;
import com.itheima.reggie.entity.OrderDetail;
import com.itheima.reggie.entity.Orders;
import com.itheima.reggie.entity.ShoppingCart;
import com.itheima.reggie.service.OrderDetailService;
import com.itheima.reggie.service.OrderService;
import com.itheima.reggie.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/order")
@Slf4j
public class OrderController {

    @Autowired
    private OrderService orderService;

    @Autowired
    private OrderDetailService orderDetailService;

    @Autowired
    private ShoppingCartService shoppingCartService;

    @PostMapping("/submit")
    public Result<String> submit(@RequestBody Orders orders){
        log.info("订单数据 : {}",orders);
        orderService.submit(orders);

        return Result.success("下单成功");
    }

    /**
     * 分页查询
     * @param page  当前页码
     * @param pageSize 当前页码显示条数
     * @param number 订单号
     * @return
     */
    @GetMapping("/page")
    public Result<Page> page(int page , int pageSize , Long number,String beginTime,String endTime){

        Page<Orders> pageInfo = new Page<>(page,pageSize);
        LambdaQueryWrapper<Orders> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(number != null ,Orders::getId,number);
        queryWrapper.orderByDesc(Orders::getOrderTime);
        queryWrapper.between(beginTime != null && endTime != null ,Orders::getOrderTime,beginTime,endTime);
        orderService.page(pageInfo,queryWrapper);
        return Result.success(pageInfo);
    }


    /**
     * 修改订单状态
     * @param orders 订单对象
     * @return
     */
    @PutMapping
    public Result<String> update(@RequestBody Orders orders){
        orderService.updateById(orders);

        return Result.success("修改成功");
    }

    /**
     * 手机端分页查询
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/userPage")
    public Result<Page> page(@RequestParam int page,int pageSize){
        Page<Orders> pageInfo = new Page<>(page, pageSize);

        orderService.page(pageInfo);
        Page<OrdersDto> ordersDtoPage = new Page<>();
        BeanUtils.copyProperties(pageInfo,ordersDtoPage,"records");

        List<Orders> records = pageInfo.getRecords();
       List<OrdersDto> list =records.stream().map((item) ->{
            OrdersDto ordersDto = new OrdersDto();
            BeanUtils.copyProperties(item,ordersDto);
           LambdaQueryWrapper<OrderDetail> queryWrapper = new LambdaQueryWrapper<>();
           queryWrapper.eq(OrderDetail::getOrderId,item.getId());
           List<OrderDetail> orderDetails = orderDetailService.list(queryWrapper);
           ordersDto.setOrderDetails(orderDetails);
           return ordersDto;
        }).collect(Collectors.toList());

        ordersDtoPage.setRecords(list);
        return Result.success(ordersDtoPage);

    }

    @PostMapping("/again")
    public Result<String>  again(@RequestBody Orders orders){
       //根据订单id  获取订单对象

        LambdaQueryWrapper<OrderDetail> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(OrderDetail::getOrderId,orders.getId());
        List<OrderDetail> list = orderDetailService.list(queryWrapper);


        //获取当前登录的用户id
        Long userId = BaseContext.getCurrentId();

        List<ShoppingCart> shoppingCarts = list.stream().map((item) -> {
            ShoppingCart shoppingCart = new ShoppingCart();
            BeanUtils.copyProperties(item, shoppingCart, "id");
            shoppingCart.setUserId(userId);
            shoppingCart.setCreateTime(LocalDateTime.now());
            return shoppingCart;
        }).collect(Collectors.toList());

        shoppingCartService.saveBatch(shoppingCarts);

        return Result.success("添加成功");
    }
}
